<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="menus" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 200%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>

      <el-table-column prop="name" label="菜单名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="component" label="菜单组件路径" width="160" sortable>
      </el-table-column>
      <el-table-column prop="url" label="路径" width="160" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="160" sortable>
      </el-table-column>
      <el-table-column prop="index" label="排序索引" width="160" sortable>
      </el-table-column>
      <el-table-column prop="父菜单id" label="parent_id" width="160" sortable>
      </el-table-column>
      <el-table-column prop="intro" label="简介" width="160" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="160" :formatter="formatState" sortable>
        <template slot-scope="scope">
            <span style="color: green" v-if="scope.row.state==true">启用</span>
          <span style="color: red" v-else-if="scope.row.state==false">禁用</span>
          <span style="color: darkslategray" v-else>未知</span>
        </template>
      </el-table-column>




			<el-table-column label="操作" width="160">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--  分页栏-->
      <el-pagination layout="prev, pager, next" @current-change="getMenus"
                     :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
      </el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="菜单名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="菜单路径" prop="component">
          <el-input v-model="editForm.component" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径" prop="url">
          <el-input v-model="editForm.url" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="editForm.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="索引" prop="index">
          <el-input v-model="editForm.index" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="intro">
          <el-input v-model="editForm.intro" auto-complete="off"></el-input>
        </el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" :label=true>启用</el-radio>
						<el-radio class="radio" :label=false>禁用</el-radio>
					</el-radio-group>
				</el-form-item>
<!--        菜单树-->
        <el-form-item label="菜单">
          <el-cascader v-model="editForm.parent_id"
                       :options="menuTree"
                       :props="{
            	checkStrictly: true,
                label: 'name', // 级联框显示那个属性的值
                value: 'id'    // 回显时需要,回传值的时候也需要
            }" clearable>
          </el-cascader>
        </el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>



	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
        //菜单树
        menuTree: [],
        title:null,
				filters: {
					name: ''
				},
        //分页相关的模型数据
				menus: [],  //绑定到表格 - 显示列表数据 - 请求完成之后
				totals: 0,  //总数量
        currentPage: 1, //当前页
		    pageSize: 5,  //每页显示的条数
				listLoading: false,
				sels: [],//列表选中列
        //高级查询所需数据
        keyword: null,

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
         /* sn: [
            { required: true, message: '请输入部门编号', trigger: 'blur' }
          ],
					name: [
						{ required: true, message: '请输入部门名称', trigger: 'blur' }
					]*/
				},
				//表单数据
				editForm: {
          name:'',
          component:'',
          url:'',
          icon:'',
          index:null,
          parent_id:'',
          intro:'',
          state:true
				},


			}
		},
		methods: {
      //查询菜单树
      getMenuTree(){
        this.$http.get("/menu/menuTree").then(res=>{
          this.menuTree=res.data;
        }).catch(res =>{})
      },
      //高级查询后跳转到第一页
      keywordQuery(){
        //解决查询时当前页数没有关键字就返回空白，其实可以查询到
        this.currentPage = 1;
        this.getMenus();
      },
			//获取用户列表
			getMenus(val) {
				this.listLoading = true; //显示加载框/忙等框
        if (val != null){
          this.currentPage = val;
        }
				this.$http.post("/menu", {currentPage: this.currentPage,pageSize: this.pageSize,keyword: this.keyword})
          .then(res => {
          //给totals赋值 - 计算分页栏
          this.totals = res.data.totals;

          //赋值分页数据 - 显示当前页的分页数据
          this.menus = res.data.data;
          //隐藏加载框/忙等框
					this.listLoading = false;

				}).catch(res => {
          this.$message.error('系统繁忙，请稍后重试');
        });
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/menu/"+row.id).then(res=>{
            this.listLoading = false;//关闭忙等框
            if(res.data.success){
                this.$message({
                  message:'删除成功',
                  type:'success'
                })
            }else {
              this.$message({
                message:'删除失败',
                type:'error'
              })
            }
            //当前页改变
            this.currentPage = 1;
            this.getMenus();
          }).catch(res => {
            this.$message.error("系统错误,请稍后尝试");
          })
				}).catch();
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
        this.editForm = Object.assign({}, row);
        this.editFormVisible = true;
			},
			//显示新增界面
			handleAdd: function () {
        this.title="新增";

				this.editForm = {
          name:'',
          component:'',
          url:'',
          icon:'',
          index:null,
          parent_id:'',
          intro:'',
          state:true
				};
        this.getMenuTree()
        this.editFormVisible = true;
			},
			//点击确认提交模态框数据
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              //开启忙等框
							this.editLoading = true;
              //将表单数据放入para，因为绑定了v-model，直接使用会影响显示数据
							let para = Object.assign({}, this.editForm);

              //提交数据时上级部门数据格式做处理：
              let arr = para.parent_id;
              if(arr && arr.length==0){//判断arr数组是否为空，点击清理的时候，是[]
                para.parent_id = null;
              }
              if(arr && arr.length>0){ //
                para.parent_id = arr[arr.length-1];
              }

              //发送请求
              this.$http.put("/menu",para).then(res=>{

                //关闭忙等框
                this.editLoading = false;
                  if(res.data.success){
                    this.$message.success("操作成功");
                    this.getMenus();
                  }else{
                    this.$message.error("操作失败【500】");
                  }
                //关闭模态框
                this.editFormVisible = false;
                //刷新页面
                this.getMenus();
              }).catch(res=>{
                  this.$message.error("系统错误【400,404】");
              })

						}).catch();
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.patch("/department",ids).then(res=>{
            this.listLoading = false;
            if(this.$message.success){
              this.$message({
                message:'删除成功',
                type:'success'
              })
            }
            else {
              this.$message({
                message:'删除失败',
                type:'error'
              })
            }
            //当前页改变
            this.currentPage = 1;
            this.getMenus();
          }).catch(res=>{
            this.$message.error("系统错误,请稍后尝试");
          })
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getMenus();
		}
	}

</script>

<style scoped>

</style>